<template>
  <div class="editor-page">
    <div class="drawer" :class="{ open: drawer }">
      <div class="open-close" @click="drawer = !drawer">
        <i :class="iconInfo.icon"></i>
        <span>{{ iconInfo.text }}</span>
      </div>
      <div class="menu-box">
        <i class="el-icon-video-play" title="运行" @click="codeRun"></i>
        <i class="el-icon-refresh" title="重置" @click="codeReset(false)"></i>
      </div>
      <div class="cpc-code-edit-box">
        <cpcCodeEdit ref="refCpcCodeEdit" :content="content"></cpcCodeEdit>
      </div>
    </div>
    <iframe
      class="iframe-map"
      ref="refIframeMap"
      src="./examples/codeEdit/index.html"
      frameborder="0"
    ></iframe>
  </div>
</template>

<script lang="ts">
import { ref, defineComponent, computed, watch, onMounted } from 'vue'
import { useRoute } from "vue-router"
import cpcCodeEdit from '@/components/cpcCodeEdit.vue';
import { ElMessage } from 'element-plus'
import { EditorCtrl } from './EditorCtrl'
export default defineComponent({
  name: "EditorPage",
  data() {
    return {
      drawer: false,
      content: "",
      example: ""
    };
  },
  components: { cpcCodeEdit },
  methods: {
    async codeRun() {
      var runEnv: any = this.$refs.refIframeMap;
      var src = runEnv.src.split("?")[0];
      runEnv.src = src + "?_=" + new Date().getTime();
      runEnv.onload = () => {
        const childWindow = runEnv.contentWindow;
        const refCpcCodeEdit: any = this.$refs.refCpcCodeEdit
        const result = EditorCtrl.parseHtml(refCpcCodeEdit.getValue());
        childWindow.setContent(result);
      }
    },
    async codeReset(isRun: boolean = false) {
      !isRun && (this.content = "");
      var res = await (window as any).axios.get(`./examples/html/${this.example}.html`);
      this.content = res.data;
      isRun && this.codeRun();
    }
  },
  computed: {
    iconInfo() {
      const drawr = (this as any).drawer;
      return {
        icon: drawr ? 'el-icon-s-fold' : 'el-icon-s-unfold',
        text: drawr ? "收起" : "源码",
      }
    }
  },
  mounted() {
    const route = useRoute();
    const example:any = route.query.example
    if (!example) {
      ElMessage.error('找不到该实例代码');
      return;
    }
    this.example = example;
    this.codeReset(true);
  },
});
</script>

<style scoped lang="less">
@import url("../../styles/var.less");
.editor-page {
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  .drawer {
    @w: 70%;
    position: absolute;
    top: 0;
    left: -@w;
    width: @w;
    height: 100%;
    transition: left 300ms linear;
    background-color: @cr-bgc-base;
    &.open {
      left: 0;
    }
    .menu-box {
      width: 100%;
      height: 0.35rem;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      font-size: 0.2rem;
      > i {
        cursor: pointer;
        margin: 0 0.05rem;
      }
    }
    .cpc-code-edit-box {
      width: 100%;
      height: calc(100% - 0.35rem);
    }
    .open-close {
      position: absolute;
      top: 0;
      right: -0.6rem;
      z-index: 999;
      display: flex;
      color: #fff;
      font-size: 0.16rem;
      line-height: 0.25rem;
      cursor: pointer;
      > i {
        font-size: 0.25rem;
      }
    }
  }

  .iframe-map {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
}
</style>
